/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanterre.annuaire.forStruts;

import com.nanterre.annuaire.bd.BDInteraction;
import com.nanterre.annuaire.model.Diplome;
import com.nanterre.annuaire.model.Mention;
import com.nanterre.annuaire.model.Parcours;
import com.nanterre.annuaire.model.UE;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author albran
 */
public class AffichierRechercheApogee extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        String codeApogee = request.getParameter("recherche");
        String type = request.getParameter("type");
        if(codeApogee != null && codeApogee.length()>0){
            PrintWriter out = response.getWriter();
            BDInteraction bdi = new BDInteraction();
            if(type.equals("DOMAINE")){
            }
            else if(type.equals("DIPLOME")){
                Diplome dip = bdi.getDiplomeByApogee(codeApogee);
                List<Parcours> listParcours = bdi.getParcourByDiplomeApogee(codeApogee);
                out.println(dip.afficherDiplomeNotComplete(dip.getId()));
                out.println("<ul id=\"debutArbre\" class=\"listArbre\">");
                for(Parcours p : listParcours){
                    out.println(p.afficherParcours(dip.getId()));
                }
                out.println("</ul>");
                out.println("</li>");
            }
            else if(type.equals("PARCOURS")){
            }
            //lister les diplomes/parcours contenant l'UE choisi
            else if(type.equals("UE")){
                request.getSession().setAttribute("recherche", bdi.getUeById(codeApogee).getLibelleComplet());
                List<Diplome> listDiplomes = bdi.getDiplomeByApogeeUE(codeApogee);
                for(Diplome d : listDiplomes){
                    List<Parcours> listParcoursWithUE = new ArrayList<Parcours>();
                    List<Parcours> listParcours = bdi.getAllParcoursByParentId(d.getId());
                    for(Parcours p : listParcours){
                        if(bdi.getUeByParentIdAndApogeeUE(p.getId(), codeApogee) != null){
                            listParcoursWithUE.add(p);
                        }
                    }
                    out.println(d.afficherDiplomeNotComplete(d.getId()));
                    out.println("<ul class=\"listArbre\">");
                    for(Parcours p : listParcoursWithUE)
                        out.println(p.afficherParcours(d.getId()));
                    out.println("</ul>");
                }
            }
            //afficher les diplomes/parcours pour l'EC selectionné
            else if(type.equals("EC")){
                request.getSession().setAttribute("recherche", bdi.getEcById(codeApogee).getLibelleComplet());
                List<Diplome> listDiplomes = bdi.getDiplomeByApogeeEC(codeApogee);
                for(Diplome d : listDiplomes){
                    List<Parcours> listParcoursWithEC = new ArrayList<Parcours>();
                    List<Parcours> listParcours = bdi.getAllParcoursByParentId(d.getId());
                    for(Parcours p : listParcours){
                        if(bdi.getEcByParcourstIdAndApogeeEc(p.getId(), codeApogee) != null){
                            listParcoursWithEC.add(p);
                        }
                    }
                    out.println(d.afficherDiplomeNotComplete(d.getId()));
                    out.println("<ul class=\"listArbre\">");
                    for(Parcours p : listParcoursWithEC)
                        out.println(p.afficherParcours(d.getId()));
                    out.println("</ul>");
                    out.println("</li>");
                }
            }
            return(null);
        }
        return mapping.findForward(SUCCESS);
    }
}
